
from pinpong.board import gboard, Pin, ADC

class AnalogVoltage():
  def __init__(self, board=None, pin=None):
    if isinstance(board, Pin):
      pin = board
      board = gboard
    elif board is None:
      board = gboard
      pin = board
    self.board = board
    self.adc = ADC(pin)
    self.map = 4095
    self.aref = 3300

  def read_voltage(self):
    if self.adc.board.boardname == 'UNIHIKER' or self.adc.board.boardname == 'HANDPY':
      self.map = 4095
      self.aref = 3.3
    elif self.adc.board.boardname == 'MICROBIT':
      self.map = 1024
      self.aref = 3.3
    else:
      self.map = 1023
      self.aref = 5.0
    voltage = (((self.adc.read() / self.map) * self.aref) * 5.0)
    return round(voltage,2)
